//
//  Concentration.swift
//  Concentration
//
//  Created by chensk on 2019/6/20.
//  Copyright © 2019 chensk. All rights reserved.
//

import Foundation
class Concentration
{
    
    var indexOfOneAndOnlyFaceUpCard:Int?
    
    var cards = [Card]();
    func  chooseCard(at index:Int){
        if !cards[index].isMatched {
            if let matchIndex = indexOfOneAndOnlyFaceUpCard,matchIndex != index{
                //判断是否配对
                if cards[matchIndex].identifier == cards[index].identifier{
                    cards[matchIndex].isMatched = true;
                    cards[index].isMatched = true;
                }
                cards[index].isFaceUp = true;
                indexOfOneAndOnlyFaceUpCard = nil;
            }else{
                for flipDownIndex in cards.indices{
                    cards[flipDownIndex].isFaceUp = false;
                }
                cards[index].isFaceUp = true;
                indexOfOneAndOnlyFaceUpCard = index;
            }
            
        }
        
//        if cards[index].isFaceUp{
//            cards[index].isFaceUp = false;
//        }else{
//            cards[index].isFaceUp = true;
//        }
       
        
    }
   
    
    init(numberOfPairsOfCards: Int){
      //TODO 生成牌组
        for _  in 1...numberOfPairsOfCards{
            let card =  Card();
            
            cards.append(card);
            cards.append(card);
           // cards.append(matchingCard);
        }
      //TODO 洗牌
    }
}
